import { SkillModel } from '../../models/Skill'

export default defineEventHandler(async (event) => {
  try {
    const { id } = getRouterParams(event)
    const body = await readBody(event)
    
    if (!id) {
      return {
        success: false,
        message: '技能ID不能为空',
        data: null
      }
    }
    
    const updateData: any = {}
    if (body.category_id !== undefined) updateData.category_id = parseInt(body.category_id)
    if (body.name !== undefined) updateData.name = body.name
    if (body.level !== undefined) updateData.level = body.level
    if (body.color !== undefined) updateData.color = body.color
    if (body.description !== undefined) updateData.description = body.description
    if (body.status !== undefined) updateData.status = body.status
    if (body.sort_order !== undefined) updateData.sort_order = body.sort_order
    
    const success = await SkillModel.updateSkill(parseInt(id), updateData)
    
    if (!success) {
      return {
        success: false,
        message: '技能不存在或更新失败',
        data: null
      }
    }
    
    return {
      success: true,
      message: '更新技能成功',
      data: { id: parseInt(id) }
    }
  } catch (error: any) {
    console.error('更新技能失败:', error)
    
    return {
      success: false,
      message: `更新技能失败: ${error.message}`,
      data: null
    }
  }
})